package com.galudisu.handsonic.modules.system.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.galudisu.handsonic.base.AudiTableEntity;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Barudisshu
 */
@Entity
@Table(name = "t_sys_resource")
public class Resource extends AudiTableEntity {

    private static final long serialVersionUID = 497030539548170145L;

    @Basic
    @Column(name = "name", nullable = false, length = 100)
    private String name;
    @Basic
    @Column(name = "type", nullable = false)
    @Enumerated(EnumType.STRING)
    private ResourceType type;
    @Basic
    @Column(name = "icon", nullable = true, length = 45)
    @Enumerated(EnumType.STRING)
    private ResourceIcon icon;
    @Basic
    @Column(name = "url", nullable = true, length = 200)
    private String url;
    @Basic
    @Column(name = "permission", nullable = true, length = 45)
    private String permission;
    @Basic
    @Column(name = "sequence", nullable = true, length = 45)
    private Integer sequence;
    @Basic
    @Column(name = "available", nullable = true)
    private Boolean available;

    //---------------------------------------------------------------------


    @JsonIgnore
    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name = "parent_id")
    private Resource parentId;

    @JsonIgnore
    @OneToMany(mappedBy = "parentId")
    private Set<Resource> childs = new HashSet<>();


    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "t_sys_role_resource",
            joinColumns = {@JoinColumn(name = "resource_id", nullable = false, updatable = false)},
            inverseJoinColumns = {@JoinColumn(name = "role_id", nullable = false, updatable = false)})
    private Set<Role> roles = new HashSet<>();


    public Resource(){}

    public Resource(String name,ResourceType type){
        this.name = name;
        this.type = type;
    }


    public static enum ResourceType {
        menu("菜单"), button("按钮");

        private final String info;

        private ResourceType(String info) {
            this.info = info;
        }

        public String getInfo() {
            return info;
        }
    }

    public static enum ResourceIcon {
        desktop("desktop"),
        list("list"),
        pencil("pencil-square-o"),
        list_alt("list-alt"),
        calendar("calendar"),
        picture_o("picture-o"),
        tag("tag"),
        file_o("file-o");

        private final String icon;

        private ResourceIcon(String icon) {
            this.icon = icon;
        }

        public String getIcon() {
            return icon;
        }
    }

    // getter setter


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ResourceType getType() {
        return type;
    }

    public void setType(ResourceType type) {
        this.type = type;
    }

    public ResourceIcon getIcon() {
        return icon;
    }

    public void setIcon(ResourceIcon icon) {
        this.icon = icon;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public Integer getSequence() {
        return sequence;
    }

    public void setSequence(Integer sequence) {
        this.sequence = sequence;
    }

    public Boolean getAvailable() {
        return available;
    }

    public void setAvailable(Boolean available) {
        this.available = available;
    }

    public Resource getParentId() {
        return parentId;
    }

    public void setParentId(Resource parentId) {
        this.parentId = parentId;
    }

    public Set<Resource> getChilds() {
        return childs;
    }

    public void setChilds(Set<Resource> childs) {
        this.childs = childs;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}
